<?php


namespace app\wechat\controller;


use app\common\controller\WechatUserBase;
use app\common\model\ShopCate;

class Shop extends WechatUserBase
{
//    获取店铺列表
    public function getShop(){
        $param = input();
        $shop_cate_id = input('shop_cate_id');
        $lat = $param['latitude'];
        $lng = $param['longitude'];

        if (!empty($lat) && is_numeric($lat) && !empty($lng) && is_numeric($lng)){
            $address = getAddress($param['latitude'],$param['longitude']);
        }else{
            $addressByIp = getAddressBtIp();
            if (empty($addressByIp)){
                $this->error('获取位置失败，请打开手机定位');
            }
            $address['province'] = $addressByIp['ad_info']['province'];
            $address['city'] = $addressByIp['ad_info']['city'];
            $address['district'] = $addressByIp['ad_info']['district'];
            $lat = $addressByIp['location']['lat'];
            $lng = $addressByIp['location']['lng'];
        }


        if (empty($address)){
            $this->error('定位失败，请重新进入');
        }

        $where = [];
//        只显示营业的店铺
        $where[] = ['state','=',10];
        if (!empty($shop_cate_id)){
            $where[] = ['shop_cate_id','=',$shop_cate_id];
        }
        $where[] = ['province','=',$address['province']];
        $where[] = ['city','=',$address['city']];
        $where[] = ['area','=',$address['district']];

        if (!empty($param['keywords'])){
            $where[] = ['name','like','%'.$param['keywords'].'%'];
        }

        $data = \app\common\model\Shop::with(['shopCate'])->where($where)->field('*,(2 * 6378.137* ASIN(SQRT(POW(SIN(PI()*('.$lat.'-lat)/360),2)+COS(PI()*'.$lat.'/180)* COS(lat * PI()/180)*POW(SIN(PI()*('.$lng.'-lng)/360),2)))) as distance')->order(['distance'=>'asc','sort'=>'desc'])->paginate($param['limit'],false,['query'=>$param]);


        foreach ($data as $k=>&$v){
            $v['distance'] = number_format($v['distance'],2);
            $v['img_thumb'] = getFullPath($v['img_thumb']);
            $v['state_text'] = $v->state_text;
        }
        $data = $data->toArray();
        return $this->result($data,$data['total'],'0',['address'=>$address]);
    }

//    获取店铺分类
    public function getShopCate(){
        $data = ShopCate::where('show','=',10)->order(['sort'=>'desc'])->select();
        $data = $data->toArray();
        array_unshift($data,[
            'id'=>'',
            'name'=>'全部',
            'img'=>'/uploads/20200607/d57b67048fcf93f543ab1f1b8f94d793.png',
        ]);
        $cate = [];
        foreach ($data as $k=>&$v){
            $v['img'] = getFullPath($v['img']);
            $cate[ceil(($k+1)/8)-1][] = $v;
        }
        $this->result($cate);
    }

    public function getShopDetail(){
        $id = input('id');
        $data = \app\common\model\Shop::where('id',$id)->find();
        if (!empty($data['carousel'])){
            $data['carousel'] = explode(',',$data['carousel']);
        }
        $data['state_text'] = $data['state_text'];
        $this->result($data);
    }
}
